﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DbDoc.PowerDoc.ViewModel;
using DbDoc.BusinessObjects;
using DbDoc.PowerDoc.Model;
using DbDoc.PowerDoc;
using DbDoc.BusinessManager;

namespace DbDoc.PowerDoc.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IRequestExit
    {
        public MainWindow(Action<string,string> popupInfo, Action<string, string> popupError)
        {
            InitializeComponent();
            AppUser user = new AppUser();
            user.IsAdmin = false;
            user.Username = "Anonymous";
            user.Password = string.Empty;
            
            if(SettingsManager.RequireLogin())
            {
                Login loginWindow = new Login();
                loginWindow.ShowDialog();
                if (loginWindow.LoggedUser == null)
                    Application.Current.Shutdown();
                user = loginWindow.LoggedUser;
            }
            Setting sourceConnectionString = SettingsManager.GetSettingByKey(Constants.Settings.SOURCE_CONNECTIONSTRING);
            string rawValue = sourceConnectionString == null ? null : sourceConnectionString.RawValue;
            if (sourceConnectionString == null || string.IsNullOrWhiteSpace (rawValue))
            {
                SetupSource source = new SetupSource();
                source.ShowDialog();
                sourceConnectionString = SettingsManager.GetSettingByKey(Constants.Settings.SOURCE_CONNECTIONSTRING);
                rawValue = sourceConnectionString == null ? null : sourceConnectionString.RawValue;
            }

            //If the Source provider details are not provided, show user a message and shutdown
            while (sourceConnectionString == null || string.IsNullOrWhiteSpace(rawValue))
            {
                if (MessageBoxResult.Yes == MessageBox.Show("The source provider (the database which needs to be documented) details have not been provided. This application will not work without these details. \r\nDo you want to try again else the application will shutdown now.", "Source Provider details not provided.", MessageBoxButton.YesNo, MessageBoxImage.Error))
                {
                    SetupSource source = new SetupSource();
                    source.ShowDialog();
                    
                }
                else
                {
                    Application.Current.Shutdown();
                    break;
                }
                sourceConnectionString = SettingsManager.GetSettingByKey(Constants.Settings.SOURCE_CONNECTIONSTRING);
                rawValue = sourceConnectionString == null ? null : sourceConnectionString.RawValue;
            }
            this.DataContext = new MainViewModel(this, user,popupInfo, popupError);
        }

        //void settings_Changed(object sender, EventArgs e)
        //{
        //    MessageBox.Show("Settings Changed.");
        //}

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            MainViewModel mainViewModel = (MainViewModel)this.DataContext;
            if(e.NewValue is DbObjectModel)
                mainViewModel.SelectedObject = (DbObjectModel)e.NewValue;
        }

        public void ExitApplication()
        {
            Application.Current.Shutdown();
        }

        private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {            
            DbObjectModel selectedObject = (DbObjectModel)dgComments.SelectedItem;
            CommentHistory history = new CommentHistory(selectedObject.UnderlyingDbObject);
            history.ShowDialog ();
        }
    }
}
